#include <stdlib.h>
#include <stdio.h>
#include "DynamicArray.h"

#define true 1
#define false 0

int ArrayInit(DMArray *array)
{
    array->dp = (ElementType *)malloc(array->size * sizeof(ElementType));
    if(array->dp == NULL)
    {
        printf("ArrayInit error!\n");
        return false;
    }
    return true;
}

void FreeArray(DMArray *array)
{
    if(array->dp != NULL)
    {
        free (array->dp);
        array->dp = NULL;
    }
}
int ReallocArray(DMArray *array)
{
  ElementType *temp = array->dp;
  array->dp = (ElementType *)malloc(sizeof(ElementType) * array->size*2);
   if(array->dp == NULL)
    {
        printf("ReallointcArray error!\n");
        return false;
    }
    for (int i = 0; i < array->len; i++)
    {
        array->dp[i] = temp[i];
    }
    array->size *= 2;
    free(temp);
    return true;
}

int InsertArray(DMArray *array, ElementType element)
{
    if(array->len == array->size)
    {
       if(ReallocArray(array) == false)
       {
        printf("can not contain more elements!\n ");
        return false;
       }
    }

    array->dp[array->len] = element;
    array->len++;
    return true;
}

 